package com.monkeygroup.mygoplus.ecchat.ui.contact.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.common.DefinedView.CircleImageView;
import com.monkeygroup.mygoplus.ecchat.ui.contact.entity.ContactDTO;
import com.monkeygroup.mygoplus.ecchat.ui.contact.entity.ContactMemberDTO;

import java.util.List;

/**
 * Created by Administrator on 2017/5/24.
 */
public class ContactExpandableListAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<ContactDTO> dataList;
    private LayoutInflater mInflater;

    public ContactExpandableListAdapter(Context context, List<ContactDTO> dataList) {
        this.mContext = context;
        this.dataList = dataList;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return dataList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return dataList.get(groupPosition).getMemberList() != null ?
                dataList.get(groupPosition).getMemberList().size() : 0;
    }

    @Override
    public ContactDTO getGroup(int groupPosition) {
        return dataList.get(groupPosition);
    }

    @Override
    public ContactMemberDTO getChild(int groupPosition, int childPosition) {
        return dataList.get(groupPosition).getMemberList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {

        GroupHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_contact_group, parent, false);
            holder = new GroupHolder();
            holder.groupIcon = (ImageView) convertView.findViewById(R.id.groupIcon);
            holder.groupName = (TextView) convertView.findViewById(R.id.groupName);
            holder.groupCount = (TextView) convertView.findViewById(R.id.groupCount);

            // 0==>> 代表未展开， 1==>>代表已展开
            holder.groupIcon.setTag(0);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }

        ContactDTO contactDTO = getGroup(groupPosition);
        if (holder.groupIcon.getTag().equals(0)) {
            holder.groupIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.chat_contact_hide));
        } else {
            holder.groupIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.chat_contact_show));
        }
        holder.groupName.setText(contactDTO.getGroupName());
        holder.groupCount.setText("" + getChildrenCount(groupPosition));

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        ChildHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_contact_member, parent, false);

            holder = new ChildHolder();
            holder.layout_item = (LinearLayout) convertView.findViewById(R.id.layout_item);
            holder.layout_more = (LinearLayout) convertView.findViewById(R.id.layout_more);
            holder.member_name = (TextView) convertView.findViewById(R.id.member_name);
            holder.member_head = (CircleImageView) convertView.findViewById(R.id.member_head);

            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }

        holder.layout_item.setVisibility(View.VISIBLE);
        holder.layout_more.setVisibility(View.GONE);
        ContactMemberDTO memberDTO = getChild(groupPosition, childPosition);
        if (memberDTO.getRemark() != null && !memberDTO.getRemark().equals("")) {
            holder.member_name.setText(memberDTO.getRemark());
        } else {
            holder.member_name.setText(memberDTO.getName());
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupHolder {
        TextView groupName;
        ImageView groupIcon;
        TextView groupCount;
    }

    class ChildHolder {
        LinearLayout layout_item;
        LinearLayout layout_more;
        CircleImageView member_head;
        TextView member_name;
    }

}